<template>
  <el-container class='elheight'>
    <el-aside class='appTree'>
      <el-tree :data="allDictKeyData" :props="defaultProps" @node-click="treeClickHandle"></el-tree>
    </el-aside>
    <el-main>
      <el-container>
        <el-header class="dzy-main-header">
          <div class="btn-group-el">
            <btn-group :currentSelectedData="currentSelectedData" @btnClickCallback="btnClickCallback"></btn-group>
          </div>
          <div class="dzy-custom-search-box">
            <div>字典值名称：<el-input placeholder="请输入字典值名称" v-model="queryData.valueName" clearable class="dzy-custom-input"></el-input></div>
            <div>字典索引名称：<el-input placeholder="请输入字典索引名称" v-model="queryData.keyName"  clearable class="dzy-custom-input"></el-input></div>
            <div><el-button @click="searchHandleClick" type="primary" icon="el-icon-search">查询</el-button></div>
            <div><el-button @click="clearQueryData" type="primary" icon="el-icon-search">查询所有</el-button></div>
          </div>
        </el-header>
        <el-main>
          <app-table
            :page-data="pageData"
            :table-columns="tableColumns"
            v-on:currentChange="handleCurrentChange"
            v-on:handleCheckedRows="handleCheckedRows" />
          <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" :before-close="cancel" :close-on-click-modal="false">
            <el-form :model="formData" :disabled="showOnly" label-width="120px" size="mini">
              <el-form-item label="字典索引编码">
                <el-select v-model="formData.keyCode" placeholder="请选择字典索引编码">
                  <el-option v-for="item in allDictKeyData" :key="item.id" :value="item.keyCode" :label="item.keyName"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="字典值名称" >
                <el-input  v-model="formData.valueName" placeholder="请输入字典值名称"></el-input>
              </el-form-item>
              <el-form-item label="字典值编号" >
                <el-input  v-model="formData.valueCode" placeholder="请输入字典值编号"></el-input>
              </el-form-item>
            </el-form>
            <div v-if="!showOnly" slot="footer" class="dialog-footer">
              <el-button @click="cancel">取 消</el-button>
              <el-button type="primary" @click="save" >提 交</el-button>
            </div>
          </el-dialog>
        </el-main>
      </el-container>
    </el-main>
  </el-container>
</template>

<script>
import BtnGroup from '@/components/BtnGroup'
import AppTable from '@/components/AppTable'
import {findDictValue, createDictValue, updateDictValue, deleteDictValue, listDictKey} from '@/http/AppApi'
export default {
  data () {
    return {
      tableData: [],
      pageData: {},
      formData: {},
      queryData: {},
      allDictKeyData: [],
      dialogTitle: '',
      dialogFormVisible: false,
      currentSelectedData: [],
      showOnly: false,
      tableColumns: [
        { label: '字典值编号', prop: 'valueCode' },
        { label: '字典值名称', prop: 'valueName' },
        { label: '字典索引编号', prop: 'keyCode' },
        { label: '字典索引编号', prop: 'keyName' }
      ],
      defaultProps: {
        label: 'keyName',
        children: 'children'
      }
    }
  },
  components: {
    BtnGroup,
    AppTable
  },
  mounted () {
    this.getTableData()
    this.getAllDictKeyData()
  },
  methods: {
    save () {
      if (this.formData) {
        if (this.formData.id) {
          updateDictValue(this.formData).then(resp => {
            if (resp.code && resp.code === '200') {
              this.getTableData()
              this.cancel()
              this.$message({
                showClose: true,
                message: '修改成功',
                type: 'success'
              })
            }
          })
        } else {
          createDictValue(this.formData).then(resp => {
            if (resp.code && resp.code === '200') {
              this.getTableData()
              this.cancel()
              this.$message({
                showClose: true,
                message: '新增成功',
                type: 'success'
              })
            }
          })
        }
      }
    },
    del (rows) {
      if (rows && rows instanceof Array && rows.length > 0) {
        this.$confirm('此操作将永久删除, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          rows.forEach(row => {
            deleteDictValue(row).then(resp => {
              if (resp.code && resp.code === '200') {
                this.getTableData()
                this.$message({
                  showClose: true,
                  message: '删除成功',
                  type: 'success'
                })
              }
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    cancel () {
      this.dialogFormVisible = false
      this.formData = {}
    },
    getTableData () {
      let param = this.queryData
      findDictValue(param).then(resp => {
        if (resp.code && resp.code === '200') {
          let data = resp.data
          if (data) {
            this.pageData = data
            if (this.pageData.items && this.pageData.items instanceof Array) {
              this.tableData = this.pageData.items
            }
          }
        }
      })
    },
    handleCurrentChange (val) {
      this.queryData.offset = (val - 1) * 10
      this.getTableData()
    },
    getAllDictKeyData () {
      listDictKey().then(resp => {
        if (resp.code && resp.code === '200') {
          let data = resp.data
          if (data && data instanceof Array) {
            this.allDictKeyData = data
          }
        }
      })
    },
    btnClickCallback (callbackData) {
      if (callbackData.btnType === 'delete') {
        this.dialogFormVisible = false
        let rows = callbackData.rows
        this.del(rows)
      } else {
        if (callbackData.btnType === 'post') {
          this.formData = {}
        } else {
          let obj = JSON.stringify(callbackData.rows[0])
          this.formData = JSON.parse(obj)
        }
        this.showOnly = callbackData.sohwOnly
        this.dialogTitle = callbackData.dialogTitle
        this.dialogFormVisible = true
      }
    },
    handleCheckedRows (rows) {
      if (rows && rows instanceof Array && rows.length > 0) {
        this.currentSelectedData = rows
        this.formData = rows[0]
      } else {
        this.currentSelectedData = []
        this.formData = []
      }
    },
    treeClickHandle (data) {
      this.queryData = {}
      this.queryData.keyCode = data.keyCode
      this.getTableData()
    },
    searchHandleClick () {
      this.getTableData()
    },
    clearQueryData () {
      this.queryData = {}
      this.getTableData()
    }
  },
  computed: {},
  watch: {}
}
</script>
<style>
</style>
